package extractorCode;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import java.io.*;

public class link3Text {
	
	private final static int INITIAL_BUF_SIZE = 65536;

	public static void main(String args[]) throws Exception
	{
		
		int count=0;
		
		//String url;
		 BufferedReader reader= new BufferedReader(new FileReader("c:\\keywords-link3.txt"));
		 //for each keyword
		 String line;
		 while((line=reader.readLine())!=null)
		 {
			 count=0;
			 String[] splitarr= line.split("\t");
			 String key=splitarr[0];
			 System.out.println(key);

			 BufferedReader br;
			 if(!splitarr[1].equals("-1"))
		     br = new BufferedReader(new FileReader("C:\\Users\\Anu\\Desktop\\Website3\\link3-links\\link3-"+key+".txt"));
			 else 
				 continue;
		String url;
		while((url=br.readLine())!=null)
		{	
		//System.out.println(url);
		String[] urlSplit = url.split("\t");
		System.out.println(urlSplit[0]);
		url=urlSplit[0];
			
		try
		{
		String page = (fetchPage(url));

		System.out.println(page);
		page= page.replaceAll(">\\s*<", "><");		
		page = page.replaceAll("\t", "");
		page = page.replaceAll("\b", "");
		page = page.replaceAll("\r", "");
		page = page.replaceAll("\n", "");
		page = page.replaceAll("\r\n", "");
		//BufferedWriter wrPage = new BufferedWriter( new FileWriter("C:\\newData2\\Website3\\link3-"+key+"-"+(count)+".txt"));
		//wrPage.write(page);
		//wrPage.close();
		Matcher newsItem = Pattern.compile("<div id=\"newstext\">(.*?)<div class=\"newsmeta\">", Pattern.CASE_INSENSITIVE).matcher( page );
		
    	Matcher newsItem2 = Pattern.compile("<div id=\"left_content\">(.*?)<div class=\"comment-paging\">", Pattern.CASE_INSENSITIVE).matcher( page );

	//	Matcher newsItem3 = Pattern.compile("<hr align=\"left\" size=\"8\" width=\"100%\" color=\"#FF9428\" noshade>(.*?)<hr align=\"left\" size=\"3\" width=\"100%\" color=\"#FF9428\" noshade>", Pattern.CASE_INSENSITIVE).matcher( page );
        

		
		File f = new File("c:\\newData2\\Website3\\link3-"+key);
		try{
		f.mkdir();
		}
		catch(Exception e)
		{}
		while(newsItem.find())
		{
			System.out.println(count);
		//System.out.println(newsItem.group(1));
		String text = newsItem.group(1);
		System.out.println(text);
		text=text.replaceAll("<br />","\r\n");
		text = text.replaceAll("\\<.*?>","");
		text = text.replaceAll("[^\\p{ASCII}]", "");
		BufferedWriter wr = new BufferedWriter( new FileWriter("C:\\newData2\\Website3\\link3-"+key+"\\link3-"+key+"-"+(count++)+".txt"));
		wr.write(text);
		wr.close();
		
		//System.out.println(text);
	//	writer.write("http://newsbusters.org"+newsItem.group(1)+"\n");
		}
		
	
		

		while(newsItem2.find())
		{
			System.out.println(count);
		//System.out.println(newsItem.group(1));
		String text = newsItem2.group(1);
		//System.out.println(text);
		text=text.replaceAll("<br>","\r\n");
		text = text.replaceAll("\\<.*?>","");
		text = text.replaceAll("[^\\p{ASCII}]", "");
		BufferedWriter wr = new BufferedWriter( new FileWriter("C:\\newData2\\Website3\\link3-"+key+"\\link3-"+key+"-"+(count++)+".txt"));
		wr.write(text);
		wr.close();
		
		//System.out.println(text);
	//	writer.write("http://newsbusters.org"+newsItem.group(1)+"\n");
		}
		}
		catch(Exception e)
		{
			System.out.println(e);
		}
		
		}
		
		br.close();
		 }
	}
	
	private static String fetchPage(String urlString){
		StringBuilder buffer = new StringBuilder(INITIAL_BUF_SIZE);
		try {
			URL url = new URL(urlString);
			
			  HttpURLConnection.setFollowRedirects(false);
              HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();

              urlConn.setRequestProperty("User-agent","Mozilla/2.0.0.11");
              urlConn.connect();
              BufferedReader reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));

			//BufferedInputStream reader = new BufferedInputStream(url.openStream());
			int temp = reader.read();
			while(temp != -1){
				buffer.append((char)temp);
				temp = reader.read();
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return buffer.toString();
	}

}
